package com.uzai.console.service.dy.impl;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.uzai.common.enums.OrderReStatusEnum;
import com.uzai.common.enums.OrderStatusEnum;
import com.uzai.common.exception.BusinessException;
import com.uzai.console.common.UzaiRespVo;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.dto.dy.dyorder.DyOrderDownLoadDto;
import com.uzai.console.dto.dy.dytoken.DyTokenQuery;
import com.uzai.console.dto.feign.dyorder.DyOrderPrvPullDto;
import com.uzai.console.dto.feign.dyorder.DyOrderSettledDto;
import com.uzai.console.dto.locallive.tlbtoken.TlbTokenQuery;
import com.uzai.console.dto.dy.dyorder.DyFindUnReceicedDto;
import com.uzai.console.dto.vph.vphorder.VphFindUnReceicedDto;
import com.uzai.console.entity.DyOrder;
import com.uzai.console.entity.DyToken;
import com.uzai.console.entity.TlbToken;
import com.uzai.console.entity.VphOrder;
import com.uzai.console.mapper.DyOrderMapper;
import com.uzai.console.mapper.DyTokenMapper;
import com.uzai.console.mapper.TlbTokenMapper;
import com.uzai.console.para.DyOrderApiRemoteApiPara;
import com.uzai.console.para.DyOrderRemoteApiPara;
import com.uzai.console.service.dy.DyOrderSyncService;
import com.uzai.console.service.dy.DyTokenService;
import com.uzai.console.service.feign.DyOrderFeignService;
import com.uzai.console.service.remoteapi.DyOrderRemoteApiService;
import io.swagger.models.auth.In;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: liuqi
 * @Date: 2019/11/27 11:23
 * @Description:
 */
@Service
public class DyOrderSyncServiceImpl implements DyOrderSyncService {

    private static Logger logger = LoggerFactory.getLogger(DyOrderSyncServiceImpl.class);

    @Autowired
    private DyTokenMapper dyTokenMapper;
    @Autowired
    private TlbTokenMapper tlbTokenMapper;
    @Autowired
    private DyOrderMapper dyOrderMapper;
    @Autowired
    private DyOrderFeignService dyOrderFeignService;
    @Autowired
    private DyTokenService dyTokenService;
    @Autowired
    private DyOrderRemoteApiService dyOrderRemoteApiService;

    /**
     * 手动下载单个抖音联盟订单
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param dyOrderDownLoadDto
     * @return null
     */
    @Override
    public void downloadOrder(DyOrderDownLoadDto dyOrderDownLoadDto, Long dyTokenId) {
        //抖音token表主键ID列表
        if(dyOrderDownLoadDto.getType() == null){
            return;
        }

        //调用订单业务库服务器
        if(dyOrderDownLoadDto.getType().intValue() == 0){ //自营达人账号
            DyToken dyToken = dyTokenMapper.selectById(dyTokenId, dyOrderDownLoadDto.getMerId());
            if(dyToken != null){
                if(Tools.getInteger(dyToken.getType()).intValue() == 0){//cookie模式
                    DyOrderPrvPullDto dyOrderPrvPullDto = new DyOrderPrvPullDto();
                    dyOrderPrvPullDto.setMerId(dyToken.getMerId());
                    dyOrderPrvPullDto.setAppId(dyToken.getAppId());
                    dyOrderPrvPullDto.setStartTime(dyOrderDownLoadDto.getQueryStartTime());
                    dyOrderPrvPullDto.setEndTime(dyOrderDownLoadDto.getQueryEndTime());
                    //订单下载状态(0已付款 1已收货 2订单失效 3订单失效 4退货退款)
                    Integer downloadOrdersStatus = Tools.getInteger(dyOrderDownLoadDto.getDownloadOrderStatus());
                    if(downloadOrdersStatus.intValue() == 0){//已付款
                        //订单状态  PAY_SUCC 付款、CONFIRM 收货、SETTLE 结算、REFUND 退款
                        dyOrderPrvPullDto.setOrderStatus("PAY_SUCC");
                    }else if(downloadOrdersStatus.intValue() == 1){ //已收货
                        dyOrderPrvPullDto.setOrderStatus("CONFIRM");
                    }else if(downloadOrdersStatus.intValue() == 2){ //订单失效
                        dyOrderPrvPullDto.setOrderStatus("REFUND");
                    }else if(downloadOrdersStatus.intValue() == 3){
                        dyOrderPrvPullDto.setOrderStatus("REFUND");
                    }else if(downloadOrdersStatus.intValue() == 4){
                        dyOrderPrvPullDto.setOrderStatus("REFUND");
                    }else {
                        dyOrderPrvPullDto.setOrderStatus("SETTLE");
                    }
                    //页面查询时间类型（4订单付款时间， 5订单完成（收货）时间， 6订单结算时间
                    Integer queryTimeType = Tools.getInteger(dyOrderDownLoadDto.getQueryTimeType());
                    if(queryTimeType == 7){//更新时间条件缓存付款时间
                        queryTimeType = 4;
                    }
                    //调用下载接口参数时间类型：1付款时间、2收货时间、3结算时间
                    Integer timeType = queryTimeType - 3;
                    if(timeType.intValue() < 1 || timeType.intValue() > 3){
                        timeType = 1;
                    }
                    dyOrderPrvPullDto.setTimeType(timeType);
                    //请求订单拉取接口
                    UzaiRespVo<Boolean> uzaiRespVo =  dyOrderFeignService.prvPull(dyOrderPrvPullDto);
                    logger.info("---调用抖音自营联盟下载订单----para={},result={}", JSONObject.toJSONString(dyOrderPrvPullDto), JSONObject.toJSONString(uzaiRespVo));
                }else{//官方api联盟模式
                    DyOrderApiRemoteApiPara dyOrderApiRemoteApiPara = new DyOrderApiRemoteApiPara();
                    dyOrderApiRemoteApiPara.setAppId(dyToken.getAppId());
                    dyOrderApiRemoteApiPara.setAccessToken(dyToken.getAccessToken());
                    dyOrderApiRemoteApiPara.setTokenType(dyToken.getTokenType());
                    dyOrderApiRemoteApiPara.setBeginTime(Long.valueOf(dyOrderDownLoadDto.getQueryStartTime()));
                    dyOrderApiRemoteApiPara.setEndTime(Long.valueOf(dyOrderDownLoadDto.getQueryEndTime()));
                    dyOrderApiRemoteApiPara.setChildOrderNo(dyOrderDownLoadDto.getChildOrderNo());
                    //页面查询时间类型（4订单付款时间， 5订单完成（收货）时间， 6订单结算时间
                    Integer queryTimeType = Tools.getInteger(dyOrderDownLoadDto.getQueryTimeType());
                    Integer queryType = 4;   //订单时间查询类型(1-付款时间 4-更新时间)
                    if(queryTimeType == 4){
                        queryType = 1;
                    }
                    dyOrderApiRemoteApiPara.setQueryType(queryType);
                    dyOrderRemoteApiService.remoteDownloadApi(dyOrderApiRemoteApiPara);
                    logger.info("---调用抖音订单库联盟下载订单----para={}", JSONObject.toJSONString(dyOrderApiRemoteApiPara));
                }
            }
        }else if(dyOrderDownLoadDto.getType().intValue() == 1){ //官方共享账号（推老板）
            TlbToken tlbToken = tlbTokenMapper.selectById(dyTokenId, dyOrderDownLoadDto.getMerId());
            if(tlbToken != null){
                DyOrderRemoteApiPara dyOrderRemoteApiPara = new DyOrderRemoteApiPara();
                dyOrderRemoteApiPara.setAppId(tlbToken.getAppId());
                dyOrderRemoteApiPara.setQueryStartTime(dyOrderDownLoadDto.getQueryStartTime());
                dyOrderRemoteApiPara.setQueryEndTime(dyOrderDownLoadDto.getQueryEndTime());
                dyOrderRemoteApiPara.setChildOrderNo(dyOrderDownLoadDto.getChildOrderNo());
                dyOrderRemoteApiService.remoteDownload(dyOrderRemoteApiPara);
                logger.info("---调用抖音官方（推老板）联盟下载订单----para={}", JSONObject.toJSONString(dyOrderRemoteApiPara));
            }
        }
    }

    /**
     * 异步手动下载多个抖音联盟订单
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param dyOrderDownLoadDto
     * @return null
     */
    @Async("taskExecutor")
    @Override
    public void downloadOrderListSync(DyOrderDownLoadDto dyOrderDownLoadDto) {
        //抖音token表主键ID列表
        if(dyOrderDownLoadDto.getType() == null){
            return;
        }
        if(dyOrderDownLoadDto.getIdList() != null && dyOrderDownLoadDto.getIdList().size() > 0){
            for (Long id : dyOrderDownLoadDto.getIdList()){
                try {
                    downloadOrder(dyOrderDownLoadDto, id);
                }catch (Exception e){
                    logger.error("-------downloadOrderListSync is error dyTokenId={}, error={}", id, e.getMessage());
                }
            }
        }
    }

    /**
     * 远程调用订单库API，手动更新单个订单
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param dyOrder
     * @return null
     */
    @Async("taskExecutor")
    @Override
    public void updateDyOrder(DyOrder dyOrder) {
        //调用抖音共享联盟更新订单接口
        TlbTokenQuery tlbTokenQuery = new TlbTokenQuery();
        tlbTokenQuery.setMerId(dyOrder.getMerId());
        tlbTokenQuery.setType("dy");
        ArrayList<TlbToken> tlbOrderList =  tlbTokenMapper.findByList(tlbTokenQuery);
        if(tlbOrderList != null && tlbOrderList.size() > 0) {
            for (TlbToken tlbToken : tlbOrderList) {
                DyOrderDownLoadDto dyOrderDownLoadDto = new DyOrderDownLoadDto();
                dyOrderDownLoadDto.setMerId(dyOrder.getMerId());
                dyOrderDownLoadDto.setType(1);
                dyOrderDownLoadDto.setQueryTimeType(4);
                dyOrderDownLoadDto.setQueryStartTime(dyOrder.getPaydt() - 1);
                dyOrderDownLoadDto.setQueryEndTime(dyOrder.getPaydt() + 1);
                dyOrderDownLoadDto.setDownloadOrderStatus(dyOrder.getStatus());
                dyOrderDownLoadDto.setChildOrderNo(dyOrder.getChildOrderNo());
                downloadOrder(dyOrderDownLoadDto, tlbToken.getId());
            }
        }

        //调用抖音自营联盟更新订单接口
        DyTokenQuery dyTokenQuery = new DyTokenQuery();
        dyTokenQuery.setMerId(dyOrder.getMerId());
        ArrayList<DyToken> dyOrderList =  dyTokenMapper.findByList(dyTokenQuery);
        if(dyOrderList != null && dyOrderList.size() > 0){
            for (DyToken dyToken : dyOrderList){
                DyOrderDownLoadDto dyOrderDownLoadDto = new DyOrderDownLoadDto();
                dyOrderDownLoadDto.setMerId(dyOrder.getMerId());
                dyOrderDownLoadDto.setType(0);
                dyOrderDownLoadDto.setQueryTimeType(4);
                dyOrderDownLoadDto.setQueryStartTime(dyOrder.getPaydt() - 1);
                dyOrderDownLoadDto.setQueryEndTime(dyOrder.getPaydt() + 1);
                dyOrderDownLoadDto.setDownloadOrderStatus(dyOrder.getStatus());
                dyOrderDownLoadDto.setChildOrderNo(dyOrder.getChildOrderNo());
                downloadOrder(dyOrderDownLoadDto, dyToken.getId());
            }
        }
    }

    /**
     * 远程调用订单库API，更新7天前未收货订单
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param merId
     * @return null
     */
    @Async("taskExecutor")
    @Override
    public void updateDyOrderUnReceivedListSync(Long merId) {

        //查询7天前未收货订单
        DyFindUnReceicedDto dyFindUnReceicedDto = new DyFindUnReceicedDto();
        dyFindUnReceicedDto.setMerId(merId);
        //设置开始时间为90天前
        Integer now = DateUtil.getNowTime();
        dyFindUnReceicedDto.setQueryStartTime(now - 7776000);
        //设置结束时间为7天前
        dyFindUnReceicedDto.setQueryEndTime(now-604800);
        //查询付款状态订单（未收货）
        dyFindUnReceicedDto.setStatus(0);

        List<DyOrder> dyOrderList =  dyOrderMapper.findUnReceivedList(dyFindUnReceicedDto);
        if(dyOrderList != null){
            for (DyOrder dyOrder : dyOrderList){
                try {
                    updateDyOrder(dyOrder);
                }catch (Exception e){
                    logger.error("-------updateDyOrderUnReceivedList is error ordersn={}, error={}", dyOrder.getParentOrderNo(),e.getMessage());
                    continue;
                }
            }
        }
    }

    @Override
    public void uploadDyOrderSettingSync(Long merId, int day, int status, List<Integer> sourceStatus) {
        if (merId == null) {
            return;
        }
        //设置结束时间为day天前
        int endTime = DateUtil.getNowTime() - day * 24 * 3600;
        dyOrderMapper.updateStatusPayCompleteToSettle(null, endTime, null, merId, status, sourceStatus);
    }

    /**
     * 远程调用订单库API，更新day天前未收货订单
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param merId
     * @param day
     * @return null
     */
    @Override
    public void updateDyOrderUnReceivedListSync(Long merId, int day) {
        if(merId == null){
            return;
        }
        //查询7天前未收货订单
        DyFindUnReceicedDto dyFindUnReceicedDto = new DyFindUnReceicedDto();
        dyFindUnReceicedDto.setMerId(merId);
        //设置开始时间未90天前
        Integer now = DateUtil.getNowTime();
        dyFindUnReceicedDto.setQueryStartTime(now - 7776000);
        //设置结束时间为day天前
        int endTime = day * 24 * 3600;
        dyFindUnReceicedDto.setQueryEndTime(now - endTime);
        //查询付款状态订单（未收货）
        dyFindUnReceicedDto.setStatus(0);
        List<DyOrder> dyOrderList =  dyOrderMapper.findUnReceivedList(dyFindUnReceicedDto);
        if(dyOrderList != null){
            for (DyOrder dyOrder : dyOrderList){
                try {
                    updateDyOrder(dyOrder);
                }catch (Exception e){
                    logger.error("-------updateDyOrderUnReceivedListSync is error ordersn={}, error={}", dyOrder.getParentOrderNo(),e.getMessage());
                    continue;
                }
            }
        }
    }

    /**
     * 远程调用订单库API，更新day天前已冻结的订单
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param merId
     * @param day
     * @return null
     */
    @Override
    public void updateDyOrderFreezedListSync(Long merId, int day) {
        if(merId == null){
            return;
        }
        //查询7天前未收货订单
        DyFindUnReceicedDto dyFindUnReceicedDto = new DyFindUnReceicedDto();
        dyFindUnReceicedDto.setMerId(merId);
        //设置开始时间未90天前
        Integer now = DateUtil.getNowTime();
        dyFindUnReceicedDto.setQueryStartTime(now - 7776000);
        //设置结束时间为day天前
        int endTime = day * 24 * 3600;
        dyFindUnReceicedDto.setQueryEndTime(now - endTime);
        //查询付款状态订单（已收货）
        dyFindUnReceicedDto.setStatus(1);
        List<DyOrder> dyOrderList =  dyOrderMapper.findFreezedList(dyFindUnReceicedDto);
        if(dyOrderList != null){
            for (DyOrder dyOrder : dyOrderList){
                try {
                    updateDyOrder(dyOrder);
                }catch (Exception e){
                    logger.error("-------updateDyOrderFreezedListSync is error ordersn={}, error={}", dyOrder.getParentOrderNo(),e.getMessage());
                    continue;
                }
            }
        }
    }

    /**
     * 远程调用订单库API，更新day天前未收货订单
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param merId
     * @param startTime
     * @return null
     */
    @Override
    public void updateDyOrderUnReceivedListSync(Long merId, Integer startTime, Integer endTime, Long userId) {
        if(merId == null){
            return;
        }
        //查询7天前未收货订单
        DyFindUnReceicedDto dyFindUnReceicedDto = new DyFindUnReceicedDto();
        dyFindUnReceicedDto.setMerId(merId);
        dyFindUnReceicedDto.setQueryStartTime(startTime);
        dyFindUnReceicedDto.setQueryEndTime(endTime);
        dyFindUnReceicedDto.setUserId(userId);
        //查询付款状态订单（未收货）
        dyFindUnReceicedDto.setStatus(0);
        List<DyOrder> dyOrderList =  dyOrderMapper.findUnReceivedList(dyFindUnReceicedDto);
        if(dyOrderList != null){
            for (DyOrder dyOrder : dyOrderList){
                try {
                    updateDyOrder(dyOrder);
                }catch (Exception e){
                    logger.error("-------updateDyOrderUnReceivedListSync is error ordersn={}, error={}", dyOrder.getParentOrderNo(),e.getMessage());
                    continue;
                }
            }
        }
    }

    /**
     * 直接结算，更新day天前未收货订单
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param merId
     * @param startTime
     * @return null
     */
    @Override
    public void updateDyOrderUnReceivedListSync_settle(Long merId, Integer startTime, Integer endTime, Long userId) {
        if(merId == null){
            return;
        }
        //查询7天前未收货订单
        DyFindUnReceicedDto dyFindUnReceicedDto = new DyFindUnReceicedDto();
        dyFindUnReceicedDto.setMerId(merId);
        dyFindUnReceicedDto.setQueryStartTime(startTime);
        dyFindUnReceicedDto.setQueryEndTime(endTime);
        dyFindUnReceicedDto.setUserId(userId);
        //查询付款状态订单（未收货）
        dyFindUnReceicedDto.setStatus(0);
        List<DyOrder> dyOrderList =  dyOrderMapper.findUnReceivedList(dyFindUnReceicedDto);
        if(dyOrderList != null){
            for (DyOrder dyOrder : dyOrderList){
                try {
                    //订单状态为0-付款状态时，才能确认收货
                    if(dyOrder.getStatus() == 0){
                        //调用确认收货接口
                        DyOrderSettledDto dyOrderSettledDto = new DyOrderSettledDto();
                        dyOrderSettledDto.setMerId(dyOrder.getMerId());
                        dyOrderSettledDto.setOrderId(dyOrder.getParentOrderNo());
                        dyOrderSettledDto.setChildOrderId(dyOrder.getChildOrderNo());
                        UzaiRespVo<Object> result =  dyOrderFeignService.settled(dyOrderSettledDto);
                        logger.info("---发送修改抖音确认收货-para={}, result={}", JSONObject.toJSONString(dyOrderSettledDto), JSONObject.toJSONString(result));
                    }
                }catch (Exception e){
                    logger.error("-------updateDyOrderUnReceivedListSync_settle is error ordersn={}, error={}", dyOrder.getParentOrderNo(),e.getMessage());
                    continue;
                }
            }
        }
    }


    /**
     * 远程调用订单库API，更新day天前已冻结的订单
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param merId
     * @param startTime
     * @return null
     */
    @Override
    public void updateDyOrderFreezedListSync(Long merId, Integer startTime, Integer endTime, Long userId) {
        if(merId == null){
            return;
        }
        //查询7天前未收货订单
        DyFindUnReceicedDto dyFindUnReceicedDto = new DyFindUnReceicedDto();
        dyFindUnReceicedDto.setMerId(merId);
        dyFindUnReceicedDto.setQueryStartTime(startTime);
        dyFindUnReceicedDto.setQueryEndTime(endTime);
        dyFindUnReceicedDto.setUserId(userId);
        //查询付款状态订单（已收货）
        dyFindUnReceicedDto.setStatus(1);
        List<DyOrder> dyOrderList =  dyOrderMapper.findFreezedList(dyFindUnReceicedDto);
        if(dyOrderList != null){
            for (DyOrder dyOrder : dyOrderList){
                try {
                    updateDyOrder(dyOrder);
                }catch (Exception e){
                    logger.error("-------updateDyOrderFreezedListSync is error ordersn={}, error={}", dyOrder.getParentOrderNo(),e.getMessage());
                    continue;
                }
            }
        }
    }

    /**
     * 直接结算，更新day天前已冻结订单
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param merId
     * @param startTime
     * @return null
     */
    @Override
    public void updateDyOrderFreezedListSync_settle(Long merId, Integer startTime, Integer endTime, Long userId) {
        if(merId == null){
            return;
        }
        //查询7天前未收货订单
        DyFindUnReceicedDto dyFindUnReceicedDto = new DyFindUnReceicedDto();
        dyFindUnReceicedDto.setMerId(merId);
        dyFindUnReceicedDto.setQueryStartTime(startTime);
        dyFindUnReceicedDto.setQueryEndTime(endTime);
        dyFindUnReceicedDto.setUserId(userId);
        //查询付款状态订单（已收货）
        dyFindUnReceicedDto.setStatus(1);
        List<DyOrder> dyOrderList =  dyOrderMapper.findFreezedList(dyFindUnReceicedDto);
        if(dyOrderList != null){
            for (DyOrder dyOrder : dyOrderList){
                try {
                    //先修改订单状态为0-付款状态时，才能确认收货
                    dyOrder.setStatus(OrderStatusEnum.STATUS_PAID_VALUE);
                    dyOrder.setUpdateTime(DateUtil.getNowTime());
                    dyOrderMapper.updateById(dyOrder);

                    //调用确认收货接口
                    DyOrderSettledDto dyOrderSettledDto = new DyOrderSettledDto();
                    dyOrderSettledDto.setMerId(dyOrder.getMerId());
                    dyOrderSettledDto.setOrderId(dyOrder.getParentOrderNo());
                    dyOrderSettledDto.setChildOrderId(dyOrder.getChildOrderNo());
                    UzaiRespVo<Object> result =  dyOrderFeignService.settled(dyOrderSettledDto);
                    logger.info("---发送修改抖音确认收货-para={}, result={}", JSONObject.toJSONString(dyOrderSettledDto), JSONObject.toJSONString(result));
                }catch (Exception e){
                    logger.error("-------updateDyOrderFreezedListSync_settle is error ordersn={}, error={}", dyOrder.getParentOrderNo(),e.getMessage());
                    continue;
                }
            }
        }
    }

    /**
     * 远程调用订单库API，更新day天前未收货订单
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param merId
     * @param day
     * @return null
     */
    @Override
    public void updateDyOrderRefundingListSync(Long merId, int day) {
        if(merId == null){
            return;
        }
        //查询7天前未收货订单
        DyFindUnReceicedDto dyFindUnReceicedDto = new DyFindUnReceicedDto();
        dyFindUnReceicedDto.setMerId(merId);
        //设置开始时间未90天前
        Integer now = DateUtil.getNowTime();
        dyFindUnReceicedDto.setQueryStartTime(now - 7776000);
        //设置结束时间为day天前
        int endTime = day * 24 * 3600;
        dyFindUnReceicedDto.setQueryEndTime(now - endTime);
        //查询付款状态订单（未收货）
        dyFindUnReceicedDto.setStatus(OrderStatusEnum.STATUS_REFUNDING_VALUE);
        List<DyOrder> dyOrderList =  dyOrderMapper.findUnReceivedList(dyFindUnReceicedDto);
        if(dyOrderList != null){
            for (DyOrder dyOrder : dyOrderList){
                try {
                    updateDyOrder(dyOrder);
                }catch (Exception e){
                    logger.error("-------updateDyOrderRefundingListSync is error ordersn={}, error={}", dyOrder.getParentOrderNo(),e.getMessage());
                    continue;
                }
            }
        }
    }

    /**
     * 远程调用订单库API，更新day天前维权中订单
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param merId
     * @param startTime
     * @return null
     */
    public void updateDyOrderRefundingListSync(Long merId, Integer startTime, Integer endTime, Long userId) {
        if(merId == null){
            return;
        }
        //查询7天前未收货订单
        DyFindUnReceicedDto dyFindUnReceicedDto = new DyFindUnReceicedDto();
        dyFindUnReceicedDto.setMerId(merId);
        dyFindUnReceicedDto.setQueryStartTime(startTime);
        dyFindUnReceicedDto.setQueryEndTime(endTime);
        dyFindUnReceicedDto.setUserId(userId);
        //查询维权中订单
        dyFindUnReceicedDto.setStatus(OrderStatusEnum.STATUS_REFUNDING_VALUE);
        List<DyOrder> dyOrderList =  dyOrderMapper.findUnReceivedList(dyFindUnReceicedDto);
        if(dyOrderList != null){
            for (DyOrder dyOrder : dyOrderList){
                try {
                    updateDyOrder(dyOrder);
                }catch (Exception e){
                    logger.error("-------updateDyOrderRefundingListSync is error ordersn={}, error={}", dyOrder.getParentOrderNo(),e.getMessage());
                    continue;
                }
            }
        }
    }

    /**
     * 直接结算，更新day天前未收货订单
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param merId
     * @param startTime
     * @return null
     */
    @Override
    public void updateDyOrderRefundingListSync_settle(Long merId, Integer startTime, Integer endTime, Long userId) {
        if(merId == null){
            return;
        }
        //查询7天前未收货订单
        DyFindUnReceicedDto dyFindUnReceicedDto = new DyFindUnReceicedDto();
        dyFindUnReceicedDto.setMerId(merId);
        dyFindUnReceicedDto.setQueryStartTime(startTime);
        dyFindUnReceicedDto.setQueryEndTime(endTime);
        dyFindUnReceicedDto.setUserId(userId);
        //查询付款状态订单（未收货）
        dyFindUnReceicedDto.setStatus(OrderStatusEnum.STATUS_REFUNDING_VALUE);
        //根据订单返利状态查询
        dyFindUnReceicedDto.setReStatusList(Lists.newArrayList(OrderReStatusEnum.RE_STATUS_NO_VALUE, OrderReStatusEnum.RE_STATUS_FROZEN_VALUE));
        List<DyOrder> dyOrderList =  dyOrderMapper.findUnReceivedList(dyFindUnReceicedDto);
        if(dyOrderList != null){
            for (DyOrder dyOrder : dyOrderList){
                try {
                    //先修改订单状态为0-付款状态时，才能确认收货
                    dyOrder.setStatus(OrderStatusEnum.STATUS_PAID_VALUE);
                    dyOrder.setUpdateTime(DateUtil.getNowTime());
                    dyOrderMapper.updateById(dyOrder);

                    //调用确认收货接口
                    DyOrderSettledDto dyOrderSettledDto = new DyOrderSettledDto();
                    dyOrderSettledDto.setMerId(dyOrder.getMerId());
                    dyOrderSettledDto.setOrderId(dyOrder.getParentOrderNo());
                    dyOrderSettledDto.setChildOrderId(dyOrder.getChildOrderNo());
                    UzaiRespVo<Object> result =  dyOrderFeignService.settled(dyOrderSettledDto);
                    logger.info("---发送修改抖音确认收货-para={}, result={}", JSONObject.toJSONString(dyOrderSettledDto), JSONObject.toJSONString(result));
                }catch (Exception e){
                    logger.error("-------updateDyOrderRefundingListSync_settle is error ordersn={}, error={}", dyOrder.getParentOrderNo(),e.getMessage());
                    continue;
                }
            }
        }
    }

}
